using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;

namespace miciv
{
    public class StringTableManager
    {
        protected CoreContext m_coreContext;
        protected Hashtable m_htblStringTables;
        protected StringTable m_defaultStringTable;
        protected StringTable m_fallbackStringTable;
        protected string m_strDefaultLanguage;
        protected string m_strFallbackLanguage;

        public StringTableManager(CoreContext _coreContext)
        {
            this.m_coreContext = _coreContext;
            this.m_htblStringTables = new Hashtable();
        }

        public StringTable GetStringTable(string _strLanguage)
        {
            if (false == this.m_htblStringTables.ContainsKey(_strLanguage))
            {
                this.m_htblStringTables[_strLanguage] = new StringTable();
            }
            return this.m_htblStringTables[_strLanguage] as StringTable;
        }

        public void SetDefaultLanguage(string _strLanguage, string _strFallbackLanguage)
        {
            this.m_strDefaultLanguage = _strLanguage;
            this.m_defaultStringTable = GetStringTable(_strLanguage);
            this.m_strFallbackLanguage = _strFallbackLanguage;
            this.m_fallbackStringTable = GetStringTable(_strFallbackLanguage);
        }

        public StringInfo GetString(string _strID)
        {
            StringInfo si = m_defaultStringTable.GetString(_strID);
            if (null == si)
            {
                si = m_fallbackStringTable.GetString(_strID);
            }
            return si;
        }

        public bool LoadStringTable(string _strLocation)
        {
            _strLocation = this.m_coreContext.CoreManager.GetExistingFile(_strLocation);
            XmlDocument xmlDoc = this.m_coreContext.CoreManager.LoadXml(_strLocation, true);
            XmlTraverser xmlTraverser = new XmlTraverser(xmlDoc.DocumentElement);
            XmlListenerLocalization locCreator = new XmlListenerLocalization(this);

            locCreator.Initialize(this.m_coreContext, xmlTraverser);
            locCreator.Attach();
            xmlTraverser.Traverse();
            locCreator.Detach();

            return true;
        }
    }
}
